import dearpygui.dearpygui as dpg
from PID import Pid

dpg.create_context()

# creating data
datax_test = []
datay_test = []


def update_data_test():
    global datax_test, datay_test
    Sp = dpg.get_value("Sp")
    Kp = dpg.get_value("Kp")
    Ki = dpg.get_value("Ki")
    Kd = dpg.get_value("Kd")
    datax_test = list(range(5))
    datay_test = [0 for _ in range(5)]
    # update data
    pid = Pid(Kp, Ki, Kd, Sp)
    for i in range(5, 100):
        datax_test.append(i)
        datay_test.append(pid.calculate())

    dpg.set_value("series_TestModel", [datax_test, datay_test])
    dpg.fit_axis_data("x_axis_TestModel")
    dpg.fit_axis_data("y_axis_TestModel")


with dpg.window(tag="Primary Window", height=-1):
    with dpg.theme(tag="plot_theme_TestModel"):
        with dpg.theme_component(dpg.mvLineSeries):
            dpg.add_theme_color(
                dpg.mvPlotCol_Line, (255, 255, 50), category=dpg.mvThemeCat_Plots
            )
            dpg.add_theme_style(
                dpg.mvPlotStyleVar_Marker,
                dpg.mvPlotMarker_Square,
                category=dpg.mvThemeCat_Plots,
            )
            dpg.add_theme_style(
                dpg.mvPlotStyleVar_MarkerSize, 3, category=dpg.mvThemeCat_Plots
            )

    with dpg.plot(label="Simulate", height=400, width=-1):
        # optionally create legend
        dpg.add_plot_legend()

        # REQUIRED: create x and y axes
        dpg.add_plot_axis(dpg.mvXAxis, label="x", tag="x_axis_TestModel")
        dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis_TestModel")

        # series belong to a y axis
        dpg.add_line_series(datax_test, datay_test, parent="y_axis_TestModel", tag="series_TestModel")
        # dpg.set_axis_limits("x_axis_TestModel", -10, 110)
        # dpg.set_axis_limits("y_axis_TestModel", -10, 510)
    dpg.bind_item_theme("series_TestModel", "plot_theme_TestModel")

    with dpg.group(horizontal=True):
        dpg.add_text("Set Point")
        dpg.add_slider_int(
            tag="Sp", default_value=350, max_value=500, width=-1, callback=update_data_test
        )

    with dpg.group(horizontal=True):
        dpg.add_text("Kp")
        dpg.add_slider_float(
            tag="Kp",
            default_value=0.04,
            min_value=-1,
            max_value=1,
            width=-1,
            callback=update_data_test,
        )

    with dpg.group(horizontal=True):
        dpg.add_text("Ki")
        dpg.add_slider_float(
            tag="Ki",
            default_value=0.7,
            min_value=-2,
            max_value=2,
            width=-1,
            callback=update_data_test,
        )

    with dpg.group(horizontal=True):
        dpg.add_text("Kd")
        dpg.add_slider_float(
            tag="Kd",
            default_value=0.015,
            min_value=-2,
            max_value=2,
            width=-1,
            callback=update_data_test,
        )

update_data_test()

dpg.create_viewport(title="PID Simulate", width=700, height=550)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("Primary Window", True)
dpg.start_dearpygui()
dpg.destroy_context()
